//主页商品功能

$(function () {
    init();
});

function init() {

    refreshPhoneList();
    refreshGoodsList();
    refreshIntelligenceList();

}

function refreshGoodsList() {
    //发起ajax http请求获取到商品列表数据
    httpGet("/goodsListServlet",showGroceriesList);

    function showGroceriesList(groceriesList) {
        //业务内容清空
        $("#div_groceries_list").html("");

        let groceriesListHtml = "";
        //获取模板
        let groceriesTemplate = $("#template_goods_info").html();
        groceriesList.forEach(groceries=>{
           //替换模板标签内容
           let groceriesHtml = groceriesTemplate
               .replaceAll("{{goodsUrl}}", groceries.goodsUrl)
               .replaceAll("{{goodsName}}", groceries.goodsName)
               .replaceAll("{{goodsPrice}}", groceries.goodsPrice)
               .replaceAll("{{id}}", groceries.id);
           //拼接HTML
            groceriesListHtml += groceriesHtml;
        });
        //填充html到指定位置
        $("#div_groceries_list").html(groceriesListHtml);
    }
}



function refreshPhoneList() {
    //发起ajax http请求获取到商品列表数据
    httpGet("/phoneListServlet",showPhoneList);

    function showPhoneList(phoneList) {
        //业务内容清空
        $("#div_phone_list").html("");

        let phoneListHtml = "";
        //获取模板
        let phoneTemplate = $("#template_phone_info").html();
        phoneList.forEach(phone=>{
           //替换模板标签内容
           let phoneHtml = phoneTemplate
               .replaceAll("{{goodsUrl}}", phone.goodsUrl)
               .replaceAll("{{goodsName}}", phone.goodsName)
               .replaceAll("{{goodsPrice}}", phone.goodsPrice)
               .replaceAll("{{id}}", phone.id);
           //拼接HTML
            phoneListHtml += phoneHtml;
        });
        //填充html到指定位置
        $("#div_phone_list").html(phoneListHtml);
    }
}



function refreshIntelligenceList() {
    //发起ajax http请求获取到商品列表数据
    httpGet("/intelligenceListServlet",showIntelligenceList);

    function showIntelligenceList(intelligenceList) {
        //业务内容清空
        $("#div_intelligence_list").html("");

        let intelligenceListHtml = "";
        //获取模板
        let intelligenceTemplate = $("#template_intelligence_info").html();
        intelligenceList.forEach(intelligence=>{
            //替换模板标签内容
            let intelligenceHtml = intelligenceTemplate
                .replaceAll("{{goodsUrl}}", intelligence.goodsUrl)
                .replaceAll("{{goodsName}}", intelligence.goodsName)
                .replaceAll("{{goodsPrice}}", intelligence.goodsPrice)
                .replaceAll("{{id}}", intelligence.id);
            //拼接HTML
            intelligenceListHtml += intelligenceHtml;
        });
        //填充html到指定位置
        $("#div_intelligence_list").html(intelligenceListHtml);
    }
}


let goodsList = [];

function addCart(obj) {

    let id = $(obj).attr("type");
    let goodsName = $(obj).parent().siblings("h3.goods_name").html();
    let price = $(obj).parent().siblings("h3.price").html();
    let url = $(obj).parent().parent().siblings("img").attr("src");

    let shop = {
        goodsId: id,
        imgUrl: url,
        remark: goodsName,
        price: price,
        goodsNum: 1,
        subtotal: price,
    };


    //商品信息存到数组里
    goodsList.push(shop);


    //goodsList.forEach(item=>console.log(item));
    let list = JSON.parse(sessionStorage.getItem("key"));

    //判断之前是否添加过商品到购物车
    if (list == null){
        sessionStorage.setItem("key",JSON.stringify(goodsList));
    } else {
        sessionStorage.setItem("key",JSON.stringify(list.concat(goodsList)));
    }
    window.alert("添加成功");

    goodsList = [];
    // cartList.push({
    //     id: id,
    //     imgUrl: url,
    //     remark: goodsName,
    //     price: price,
    //     number: 1,
    //     subtotal: price,
    // });


}

